﻿Imports System.Xml.Serialization

Namespace Commands
    Public Class ContractUpdate
        Inherits Framework.Net.CommandBase

        Private Const mRequestCode As String = "ContractUpdate"
        Private Const mReplyCode As String = ""

        Public Sub New()
            MyBase.New(mRequestCode, mReplyCode)
        End Sub

        Public Class Message
            Public Contract As Entities.Backup.ReceivedContract
        End Class

        Friend Shared Sub SendNotification(ByVal node As Framework.Net.Node, ByVal contract As Entities.Backup.IssuedContract)
            Dim clsMsg As New Message
            clsMsg.Contract = New Entities.Backup.ReceivedContract
            clsMsg.Contract.Comment = contract.Comment
            clsMsg.Contract.UniqueId = contract.UniqueId
            clsMsg.Contract.IssuedByBuddyId = 0
            clsMsg.Contract.Revoked = contract.Revoked
            clsMsg.Contract.StorageSize = contract.StorageSize

            Dim iMessageId As Integer = Framework.Net.Manager.GetNextMessageId
            node.SendCommandMessageAsync(iMessageId, mRequestCode, Framework.Serialization.ToXmlBytes(clsMsg))
        End Sub

        Public Overrides Sub HandleReply(ByVal envelope As Framework.Net.MessageEnvelope)
            Throw New NotImplementedException
        End Sub

        Public Overrides Sub HandleRequest(ByVal envelope As Framework.Net.MessageEnvelope)
            Dim clsMessage As Message = Framework.Serialization.FromXmlBytes(Of Message)(envelope.Message.Message)
            Dim clsContract As Entities.Backup.ReceivedContract = clsMessage.Contract
            clsContract.IssuedByBuddyId = envelope.Node.BuddyId

            Dim clsExisting As Entities.Backup.ReceivedContract
            clsExisting = EntityHandlers.Backup.ReceivedContracts.GetByUniqueId(clsContract.UniqueId)
            If clsExisting IsNot Nothing Then clsContract.Id = clsExisting.Id
            EntityHandlers.Backup.ReceivedContracts.Save(clsContract)
        End Sub
    End Class
End Namespace


